function showToast(message, isHint = true, icon = 'success') {
	if (isHint)
		uni.showToast({
			title: message,
			duration: 888,
			icon: icon
		});
}

export default {
	add(tableName, data, isHint = true) {
		return new Promise((resolve, reject) => {
			uni.showLoading({
				title: '处理中...'
			})
			uniCloud.callFunction({
				name: 'add',
				data: {
					t: tableName,
					data: data
				}
			}).then((res) => {
				debugger
				if (res.result.status === 200) {
					showToast(res.result.msg, isHint)
					uni.hideLoading()
					resolve(res.result);
				} else {
					showToast(res.result.msg, isHint, 'none')
					uni.hideLoading()
					reject(res);
				}
			}).catch((err) => {
				console.log(err)
				showToast(err.result.msg, isHint, 'none')
				uni.hideLoading()
				reject(err);
			})
		})
	},
	remove(tableName, id, isHint = true) {
		return new Promise((resolve, reject) => {
			uni.showLoading({
				title: '处理中...'
			})
			uniCloud.callFunction({
				name: 'remove',
				data: {
					t: tableName,
					_id: id
				}
			}).then((res) => {
				if (res.result.status === 200) {
					showToast(res.result.msg, isHint)
					uni.hideLoading()
					resolve(res);
				} else {
					showToast(res.result.msg, isHint, 'none')
					uni.hideLoading()
					reject(res);
				}
			}).catch((err) => {
				showToast(err.result.msg, isHint, 'none')
				uni.hideLoading()
				reject(err);
			})
		})
	},
	update(tableName, data, isHint = true) {
		return new Promise((resolve, reject) => {
			if (isHint) {
				uni.showLoading({
					title: '处理中...'
				})
			}
			uniCloud.callFunction({
				name: 'update',
				data: {
					t: tableName,
					data: data
				}
			}).then((res) => {
				if (res.result.status === 200) {
					showToast(res.result.msg, isHint)
					uni.hideLoading()
					resolve(res);
				} else {
					showToast(res.result.msg, isHint, 'none')
					uni.hideLoading()
					reject(res);
				}
			}).catch((err) => {
				showToast(err.result.msg, isHint, 'none')
				uni.hideLoading()
				reject(err);
			})
		})
	},
	get(tableName, data, isHint = false) {
		return new Promise((resolve, reject) => {
			if (isHint) {
				uni.showLoading({
					title: '查询中...'
				})
			}
			let limit = data.limit || 20;
			delete data.limit;
			uniCloud.callFunction({
				name: 'get',
				data: {
					t: tableName,
					limit: limit,
					data: data
				}
			}).then((res) => {
				if (res.result.status === 200) {
					showToast(res.result.msg, isHint)
					uni.hideLoading()
					resolve(res.result.data);
				} else {
					showToast(res.result.msg, isHint, 'none')
					uni.hideLoading()
					reject(res);
				}
			}).catch((err) => {
				showToast(err.result.msg, isHint, 'none')
				uni.hideLoading()
				reject(err);
			})
		})
	},
	responses(code, data) {
		return {
			code: code,
			data: data
		}
	}
}
